package com.youlu.campus.admin.complaint;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.wechat.service.impl.MerchantComplaintService;
import com.youlu.campus.entity.complaint.ComplaintCommentInfo;
import com.youlu.campus.entity.complaint.vo.QueryComplaintVO;
import com.youlu.campus.entity.complaint.vo.ReplyComplaint;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

@RestController
public class MerchantComplaintController {

    @Autowired
    private MerchantComplaintService merchantComplaintService;

    @GetMapping("/merchant-complaint/comment/detail")
    public QResult commentDetail(@RequestParam String complaintId) {
        return QResult.success(merchantComplaintService.commentDetailByComplaintId(complaintId));
    }

    /**
     * 投诉备注
     *
     * @param complaintCommentInfo
     * @return
     */
    @PostMapping("/merchant-complaint/comment/save")
    public QResult commentSave(@RequestBody ComplaintCommentInfo complaintCommentInfo) {
        complaintCommentInfo.setOptName(AuthUtils.getLoginName());
        merchantComplaintService.commentSave(complaintCommentInfo);
        return QResult.success(null);
    }


    /**
     * 上传商户号图片
     *
     * @param merchantId
     * @param file
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
   /* @PostMapping("/merchant-complaint/upload")
    public QResult uploadImage(@RequestParam String merchantId, @RequestParam MultipartFile file) throws IOException, URISyntaxException {
        return QResult.success(merchantComplaintService.upload(merchantId, file));
    }*/

    /**
     * 投诉导出
     *
     * @param queryComplaintVO
     * @return
     */
    @PostMapping("/merchant-complaint/export")
    public QResult complaintExport(@RequestBody QueryComplaintVO queryComplaintVO, HttpServletResponse response) throws UnsupportedEncodingException {
        queryComplaintVO.setCurrentUserId(AuthUtils.getUserId());
        merchantComplaintService.export(queryComplaintVO, response);
        return QResult.success(null);
    }


    /**
     * 投诉列表
     *
     * @param queryComplaintVO
     * @return
     */
    @PostMapping("/merchant-complaint/list")
    public QResult complaintList(@RequestBody QueryComplaintVO queryComplaintVO) {
        return QResult.success(merchantComplaintService.list(queryComplaintVO));
    }

    /**
     * 删除投诉
     * @param id
     * @return
     */
    @GetMapping("/merchant-complaint/delete")
    public QResult delete(@RequestParam("id") String id) {
        return QResult.success(merchantComplaintService.delete(id));
    }

    /**
     * 投诉处理完成
     *
     * @param complaintId
     * @return
     */
    @GetMapping("/merchant-complaint/complete")
    public QResult complaintComplete(@RequestParam String complaintId) {
        merchantComplaintService.completeComplaint(complaintId, AuthUtils.getUserId());
        return QResult.success(null);
    }

    /**
     * 投诉详情
     *
     * @param complaintId
     * @return
     */
    @GetMapping("/merchant-complaint/detail")
    public QResult detail(@RequestParam String complaintId) {
        return QResult.success(merchantComplaintService.detail(complaintId));
    }

    /**
     * 查询协商历史
     *
     * @param queryComplaintVO
     * @return
     */
    @PostMapping("/merchant-complaint/history")
    public QResult history(@RequestBody QueryComplaintVO queryComplaintVO) {
        return QResult.success(merchantComplaintService.history(queryComplaintVO));
    }


    @GetMapping("/merchant/list")
    public QResult merchantList() {
        return QResult.success(merchantComplaintService.findMerchantList());
    }

    /**
     * 投诉回复
     *
     * @param replyComplaint
     * @param bindingResult
     * @return
     */
    @PostMapping("/merchant-complaint/reply")
    public QResult reply(@RequestBody @Validated ReplyComplaint replyComplaint, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        replyComplaint.setReplyUserId(AuthUtils.getUserId());
        replyComplaint.setReplyUserName(AuthUtils.getLoginName());
        merchantComplaintService.reply(replyComplaint);
        return QResult.success(null);
    }

    /**
     * 批量投诉
     *
     * @param batchComplaint
     * @return
     */
    @PostMapping("/merchant-complaint/batch-process")
    public QResult batchProcessed(@RequestBody QueryComplaintVO batchComplaint) {
        batchComplaint.setCurrentUserId(AuthUtils.getUserId());
        merchantComplaintService.batchProcessed(batchComplaint);
        return QResult.success(null);
    }

}
